//
//  RepairVC.swift
//  DianXia
//
//  Created by you&me on 2017/12/20.
//  Copyright © 2017年 you&me. All rights reserved.
//

import UIKit

class RepairVC: UIViewController {

    @IBOutlet weak var vcTitle: UIButton!
    @IBOutlet weak var reasonText: UITextField!
    @IBOutlet weak var selectBtn: UIButton!
    @IBOutlet weak var otherReasonText: UITextField!
    @IBOutlet weak var submitBtn: UIButton!
    @IBOutlet weak var backBtn: UIButton!
    var code = ""
    var taskNo = 0
    let supportVM = SupportVM()
    let dispose = DisposeBag()
    var reasonArray = [""]

    //默认退服
    var vcType = HomeEventType.Repair  {
        didSet {
            switch vcType {
            case .Repair:
             reasonArray = ["电源故障","电机故障","风扇故障","门锁故障","显示屏故障","读卡器故障","其他"]
             submitBtn.setTitle("确认提交", for: .normal)
                vcTitle.setTitle("维  护", for: .normal)
            case .Unlive:
             reasonArray = ["机器故障","退出商业区","物业协调","人流量少","其他"]
            submitBtn.setTitle("确认退服", for: .normal)
            vcTitle.setTitle("退  服", for: .normal)
            default:
                break;
            }
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        supportVM.repair(code, taskNo) // 申请维修

        //是否链接成功
        supportVM.isRobotOpen.asDriver()
            .drive(self.ex_robotState)
            .disposed(by: dispose)

        //提交
        submitBtn.rx.tap
            .subscribe({ [weak self] _ in
                var logText = self?.reasonText.text
                if logText == "其他" {
                    logText = "其他" + (self?.otherReasonText.text)!
                }
                self?.supportVM.repairCompleteRequest((self?.code)!, logText ?? "") })
            .disposed(by: dispose)

        supportVM.okUnlive.asObservable()
            .subscribe(onNext: { (unLive) in
                if unLive {
                    BCShowMessage.shareMessage.showTipMsg("退服成功", time: 1)
                    let vc = UIStoryboard.initialViewControllerFromStoryboard(.Home) as! UIViewController
                    UIApplication.shared.keyWindow?.rootViewController = vc
                }
            })
            .disposed(by: dispose)
        
        setUI()
    }

    func setUI() {
        let listTabY = reasonText.frame.maxY + 3.0
        let listTabX = reasonText.frame.minX
        let listH = reasonArray.count * 30
        let listW = 180.0
        let listRect = CGRect(x: listTabX, y: listTabY, width: CGFloat(listW), height: CGFloat(listH))
        let listView = ListView.init(frame: listRect)
        listView.listDataSource = reasonArray
        listView.backgroundColor = UIColor.purple
        listView.isHidden = true
        self.view.addSubview(listView)
        listView.selectRow.asObservable()
                .subscribe(onNext: { (selectText) in
                   listView.isHidden = true
                   self.reasonText.text = selectText
                    if selectText == "其他" {
                        self.otherReasonText.isHidden = false
                    }else {
                        self.otherReasonText.isHidden = true
                    }
                })
                .disposed(by: dispose)

        backBtn.rx.tap
            .subscribe({ _ in
                let vc = UIStoryboard.initialViewControllerFromStoryboard(.Home) as! UIViewController
                UIApplication.shared.keyWindow?.rootViewController = vc
            })
            .disposed(by: dispose)

        selectBtn.rx.tap
            .subscribe({ _ in
               listView.isHidden = !listView.isHidden
            })
            .disposed(by: dispose)

    }

}
